const koa = require('koa')
const mongoose = require('mongoose')
 const schedule = require('node-schedule')

const views = require('koa-views')
const { resolve } = require('path')

const { connect, initSchemas, initAdmin } = require('./database/init')

const { getNowDate } = require('./lib/util.js')

const R = require('ramda')
const MIDDLEWARES = ['common', 'router','parcel']

// const router = require('./routes')

const useMiddlewares = (app) => {
    R.map(
        R.compose(
            R.forEachObjIndexed(
                initWith => initWith(app)
            ),
            require,
            name => resolve(__dirname, `./middlewares/${name}`)
        )
    )(MIDDLEWARES)
}


;
(async() => {
    await connect()
    initSchemas()
    await initAdmin()
    // const Movie = mongoose.model('Movie')
    // const movies = await Movie.find({})

    // console.log('------------------')
    // console.log(movies)

    // 第一步，爬取页面上的数据
    // require('./tasks/movie')
    // 
    // 第二步，根据api完善爬取到的数据信息
    // require('./tasks/api')
    // 
    // 第三步 视频预告片地址的获取和图片获取拼接，存入数据库
    // require('./tasks/trailer')
    // 
    // 数据上传到七牛云
    // require('./tasks/qiniu')

    
    let rule = new schedule.RecurrenceRule()
    rule.hour =0;
    rule.minute =0;
    rule.second =0;
    schedule.scheduleJob(rule,()=>{
        console.log('-----------------爬取页面上的数据-------------')
        console.log('----------------------'+ getNowDate() +'--------------------------')
        // 第一步，爬取页面上的数据
        require('./tasks/movie')
    })


    let rule2 = new schedule.RecurrenceRule()
    rule2.hour =0;
    rule2.minute =5;
    rule2.second =0;
    schedule.scheduleJob(rule2,()=>{
        console.log('-----------------根据api完善爬取到的数据信息----------------')
        console.log('----------------------'+ getNowDate() +'--------------------------')
        // 第二步，根据api完善爬取到的数据信息
        require('./tasks/api')
    })



    let rule3 = new schedule.RecurrenceRule()
    rule3.hour =0;
    rule3.minute =10;
    rule3.second =0;
    schedule.scheduleJob(rule3,()=>{
        console.log('-----------------视频预告片地址的获取和图片获取拼接，存入数据库----------------')
        console.log('----------------------'+ getNowDate() +'--------------------------')
        // 第三步 视频预告片地址的获取和图片获取拼接，存入数据库
        require('./tasks/trailer')
    })

    let rule4 = new schedule.RecurrenceRule()
    rule4.hour =0;
    rule4.minute =15;
    rule4.second =0;
    schedule.scheduleJob(rule4,()=>{
        console.log('-----------------数据上传到七牛云----------------')
        console.log('----------------------'+ getNowDate() +'--------------------------')
        // 数据上传到七牛云
        require('./tasks/qiniu')
    })
    

    


    const app = new koa()
    await useMiddlewares(app)
    app.listen(4455)


})()






// async function start(){
//     const app = new koa()
//     await useMiddlewares(app)
//     app.listen(4455)
// }

// start()


// app.use(router.routes()).use(router.allowedMethods())

//第一个参数拼接路径 ，第二个参数后缀名
// app.use(views(resolve(__dirname, './views'), {
//     extension: 'pug'
// }))

// app.use(async(ctx, next) => {
//     await ctx.render('index', {
//         you: '这是我的哈哈哈',
//         me: 'Jace'
//     })
// })